#ifndef YHAIDA_POLLER_H
#define YHAIDA_POLLER_H
#include "../base/Utils.h"
#include "EventLoop.h"
#include <poll.h>
#include <vector>
#include <map>
#include <iterator>
namespace yhaida
{
    //前向声明
    class Channel;

    class Poller : Utils::nocapyable
    {
    public:
        //整个EventLoop维护着两个std::vector<Channel*>
        //  1.Poller里面的IO复用后fd与channel匹配，激活后放入2.中去
        //  2.EventLoop里面activeChannels
        typedef std::vector<Channel *> ChannelList;
        typedef std::vector<struct pollfd> PollerList;
        typedef std::map<int, Channel *> ChannelMap;

    public:
        Poller(EventLoop *loop);
        ~Poller()=default;

        //核心
        Timestamp poll(ChannelList *EventLoopActiveChannels, int timeout);
        
        void updataChannel(Channel *channel);
        void removeChannel(Channel* channel);

        void assertInLoopThread() { _mainEventLoop->assertInLoopThread(); }

    private:
        void fillActiveChannels(int numEvents,ChannelList* EventLoopActiveChannels)const;

        EventLoop *_mainEventLoop;
        PollerList _pollfds;
        ChannelMap _channels;
    };

}

#endif